package 赛码.马蜂窝.树;

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] split = s.split(",");
        int length = split.length;
        int i = sc.nextInt();
        int function = function(split, i);
        System.out.println(function);
    }

    private static int function(String[] split, int i) {
        int b = 0,c=0;
        if(i<split.length){
            for(int a = 0;a<split.length;a++){
                if(split[a].equals(String.valueOf(i))) b = a;//第几个
            }
            for(int a = 0;a<100;a++){
             if(b<Math.pow(2,a)); c = a;//行
            }
            if (Math.pow(2,c)>split.length) return  -1;
            else{
                c = c-1;
                int d = b - (int)Math.pow(2,c);
                return Integer.parseInt(split[(int)(Math.pow(2,(c+1) + d*2))]);
            }
        }
        return -1;
    }
}
